﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class frm_TDQLNN : Form
    {
        string temp = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public frm_TDQLNN()
        {
            InitializeComponent();
        }

        private void frm_TDQLNN_Load(object sender, EventArgs e)
        {
            Load_TDQLNN();
        }
        public void Load_TDQLNN()
        {
            dataGridView_TDQLNN.DataSource = sv.getTDQLNN();
            btn_Luu_TDQLNN.Enabled = false;
            btn_Huy_TDQLNN.Enabled = false;
        }

        private void dataGridView_TDQLNN_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_Ma_TDQLNN.Text = dataGridView_TDQLNN.CurrentRow.Cells[0].Value.ToString();
            txt_Ten_TDQLNN.Text = dataGridView_TDQLNN.CurrentRow.Cells[1].Value.ToString();
            temp = dataGridView_TDQLNN.CurrentRow.Cells[0].Value.ToString();
            txt_Ma_TDQLNN.Enabled = false;
            txt_Ten_TDQLNN.Enabled = false;
        }

        private void btn_Them_TDQLNN_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_Huy_TDQLNN.Enabled = true;
            btn_Luu_TDQLNN.Enabled = true;
            btn_Sua_TDQLNN.Enabled = false;
            btn_Them_TDQLNN.Enabled = true;
            btn_Thoat_TDQLNN.Enabled = true;
            btn_Xoa_TDQLNN.Enabled = false;
            txt_Ma_TDQLNN.Enabled = true;
            txt_Ten_TDQLNN.Enabled = true;
            txt_Ma_TDQLNN.Text = "";
            txt_Ten_TDQLNN.Text = "";
        }

        private void btn_Luu_TDQLNN_Click(object sender, EventArgs e)
        {
            string a = txt_Ma_TDQLNN.Text;
            string b = txt_Ten_TDQLNN.Text;
            ServiceReference1.TD_QUANLY_NHANUOC tdqlnn = new ServiceReference1.TD_QUANLY_NHANUOC();
            tdqlnn.Ma_TDQLNN = a;
            tdqlnn.TenTDQLNN = b;
            if (a == "" | b == "")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.check_tdqlnn(tdqlnn))
                        kq = -1;
                    else
                        kq = sv.addTDQLNN(tdqlnn);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setTDQLNN(tdqlnn);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_TDQLNN();
                    btn_Sua_TDQLNN.Enabled = true;
                    btn_Xoa_TDQLNN.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lỗi!");
                }
            }
        }

        private void btn_Thoat_TDQLNN_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát bảng này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btn_Xoa_TDQLNN_Click(object sender, EventArgs e)
        {
            ServiceReference1.TD_QUANLY_NHANUOC tdqlnn = new ServiceReference1.TD_QUANLY_NHANUOC();
            tdqlnn.Ma_TDQLNN = temp;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?", "warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteTDQLNN(tdqlnn);
                Load_TDQLNN();
                txt_Ma_TDQLNN.Text = "";
                txt_Ten_TDQLNN.Text = "";
            }
        }

        private void btn_Sua_TDQLNN_Click(object sender, EventArgs e)
        {
            flag = "sua";
            btn_Huy_TDQLNN.Enabled = true;
            btn_Luu_TDQLNN.Enabled = true;
            txt_Ten_TDQLNN.Enabled = true;
        }

        private void btn_Huy_TDQLNN_Click(object sender, EventArgs e)
        {
            btn_Huy_TDQLNN.Enabled = false;
            btn_Luu_TDQLNN.Enabled = false;
            btn_Sua_TDQLNN.Enabled = true;
            btn_Xoa_TDQLNN.Enabled = true;
        }
    }
}
